package com.pospi.pai.yunpos.more;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lany.sp.SPHelper;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.dao.GoodsDao;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.Base2Activity;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.util.GetData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by acer on 2018/9/7.
 */

public class PopChangeActivity extends Base2Activity {
    @Bind(R.id.list_goods)
    ListView list_goods;
    @Bind(R.id.tv_rq)
    TextView tv_rq;
    private GoodsDao dao;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_pop_change);
        ButterKnife.bind(this);
        dao = new GoodsDao(this);
        setTitle("促销价调整商品查询");
        init();
    }

    private void init() {
        tv_rq.setText(GetData.getYYMMDDTime());
        adapter = new MyAdapter();
        list_goods.setAdapter(adapter);
    }

    @Override
    public void onMenuClick(View view) {
        super.onMenuClick(view);
    }
    @OnClick({R.id.iv_search,R.id.tv_rq})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
                search(tv_rq.getText().toString());
                break;
            case R.id.tv_rq:
                showDate(tv_rq);
                break;
        }
    }

    private List<PopBeen> dtos = new ArrayList<>();
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return dtos.size();
        }

        @Override
        public Object getItem(int i) {
            return dtos.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(PopChangeActivity.this).inflate(R.layout.scale_bill_item, null);
                holder.name = view.findViewById(R.id.name);
                holder.tv_dj = view.findViewById(R.id.tv_dj);
                holder.tv_dw = view.findViewById(R.id.tv_dw);
                holder.tv_xh = view.findViewById(R.id.tv_xh);
                holder.code = view.findViewById(R.id.code);
                holder.tv_plu = view.findViewById(R.id.tv_plu);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            holder.tv_plu.setVisibility(View.GONE);
            PopBeen bill = dtos.get(i);
            holder.name.setText(bill.getName());
            holder.code.setText(bill.getBarcode());
            holder.tv_dj.setText(String.valueOf(bill.getLsj()));
            holder.tv_dw.setText(bill.getPoplsj());
            holder.tv_xh.setText(String.valueOf(i + 1));
            return view;
        }

        class Holder {
            TextView name;
            TextView tv_dj;
            TextView tv_dw;
            TextView code;
            TextView tv_xh;
            TextView tv_plu;

        }
    }
    private void search(String rq ){
        showLodingDialog("请求中..");
        Map<String, String> params = new HashMap<>();
        params.put("model", "pos.mreport");
        params.put("fun", "find_cxjtz");
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("rq", "");
        params.put("market", SPHelper.getInstance().getString(Constant.STORE_ID));
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                try {
                    JSONObject object = new JSONObject(Response);
                    JSONArray array = object.getJSONArray("result");
                    final String msg = object.getString("errMsg");
                    String code = object.getString("errCode");
                    if (!code.equals("100")) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(PopChangeActivity.this, msg, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                    List<PopBeen> lsjBeens = new Gson().fromJson(array.toString(), new TypeToken<List<PopBeen>>() {
                    }.getType());
                    if (lsjBeens != null) {
                        dtos.clear();
                        dtos.addAll(lsjBeens);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (dtos.size() == 0) {
                                    Toast.makeText(PopChangeActivity.this, "未查询到数据", Toast.LENGTH_SHORT).show();
                                }
                                closeLodindDialog();
                                adapter.notifyDataSetChanged();
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    closeLodindDialog();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(PopChangeActivity.this, "请求异常", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });
    }
    private void showDate(final TextView tv) {
        final Calendar c = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                c.set(year, monthOfYear, dayOfMonth);
                tv.setText(DateFormat.format("yyyy-MM-dd", c));
            }
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

}
